package slh.movie;

import slh.ConSql;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MovieDao {
private Connection conn;
    public MovieDao(Connection conn) {
        this.conn=conn;
    }

    public static List<Movie> searchMovies(String keyword) throws SQLException {
        List<Movie> movies = new ArrayList<>();
        String sql = "SELECT * FROM Movie WHERE TITLE LIKE ? OR DESCRIPTION LIKE ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, "%" + keyword + "%");
            stmt.setString(2, "%" + keyword + "%");

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Movie movie = mapResultSetToMovie(rs);
                    movies.add(movie);
                }
            }
        }
        return movies;
    }
    // 批量查询电影的方法
    public static List<Movie> getMoviesByIds(List<Integer> ids) throws SQLException {
        if (ids == null || ids.isEmpty()) {
            return Collections.emptyList();
        }

        String sql = "SELECT * FROM Movie WHERE MOVIE_ID IN (" +
                String.join(",", Collections.nCopies(ids.size(), "?")) + ")";

        List<Movie> movies = new ArrayList<>();
        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            // 设置参数
            for (int i = 0; i < ids.size(); i++) {
                stmt.setInt(i + 1, ids.get(i));
            }

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    movies.add(mapResultSetToMovie(rs));
                }
            }
        }
        return movies;
    }
    public static Movie getMovieById(int id) throws SQLException {
        String sql = "SELECT * FROM Movie WHERE MOVIE_ID = ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);

            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    return mapResultSetToMovie(rs);
                }
            }
        }
        return null;
    }

    // 新增方法：更新电影评分
    public static boolean updateMovieRating(int movieId, double newScore) throws SQLException {
        String sql = "UPDATE Movie SET SCORE = ?, RATING_COUNT = RATING_COUNT + 1 WHERE MOVIE_ID = ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setDouble(1, newScore);
            stmt.setInt(2, movieId);

            int affectedRows = stmt.executeUpdate();
            return affectedRows > 0;
        }
    }

    // 新增方法：获取热门电影（按评分和评分人数排序）
    public static List<Movie> getPopularMovies(int limit) throws SQLException {
        List<Movie> movies = new ArrayList<>();
        String sql = "SELECT * FROM Movie ORDER BY SCORE DESC, RATING_COUNT DESC LIMIT ?";

        try (Connection conn = ConSql.getConnection();
             PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, limit);

            try (ResultSet rs = stmt.executeQuery()) {
                while (rs.next()) {
                    Movie movie = mapResultSetToMovie(rs);
                    movies.add(movie);
                }
            }
        }
        return movies;
    }

    public List<Movie> getAllMovies() throws SQLException {
        List<Movie> movies = new ArrayList<>();
        String sql = "SELECT * FROM Movie";

        try (Connection conn = ConSql.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {

            while (rs.next()) {
                Movie movie = new Movie();
                movie.setMovieId(rs.getInt("MOVIE_ID"));
                movie.setTitle(rs.getString("TITLE"));
                movie.setDirector(rs.getString("DIRECTOR"));
                movie.setReleaseDate(rs.getDate("RELEASE_DATE"));
                movie.setActors(rs.getString("ACTORS"));
                movie.setGenre(rs.getString("GENRE"));
                movie.setCountry(rs.getString("COUNTRY"));
                movie.setLanguage(rs.getString("LANGUAGE"));
                movie.setDuration(rs.getInt("DURATION"));
                movie.setDescription(rs.getString("DESCRIPTION"));
                movie.setCoverImage(rs.getString("COVER_IMAGE"));
                // 假设我们添加了一个评分字段
                movie.setScore(rs.getDouble("RATING"));

                movies.add(movie);
            }
        }
        return movies;
    }
    // 辅助方法：将ResultSet映射到Movie对象
    private static Movie mapResultSetToMovie(ResultSet rs) throws SQLException {
        Movie movie = new Movie();
        movie.setMovieId(rs.getInt("MOVIE_ID"));
        movie.setTitle(rs.getString("TITLE"));
        movie.setDirector(rs.getString("DIRECTOR"));
        movie.setReleaseDate(rs.getDate("RELEASE_DATE"));
        movie.setActors(rs.getString("ACTORS"));
        movie.setGenre(rs.getString("GENRE"));
        movie.setCountry(rs.getString("COUNTRY"));
        movie.setLanguage(rs.getString("LANGUAGE"));
        movie.setDuration(rs.getInt("DURATION"));
        movie.setDescription(rs.getString("DESCRIPTION"));
        movie.setCoverImage(rs.getString("COVER_IMAGE"));
        movie.setScore(rs.getDouble("SCORE"));
        movie.setRatingCount(rs.getInt("RATING_COUNT"));
        return movie;
    }
}